/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.maze.display.swing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import org.maze.Maze;

/**
 *
 * @author Thomas
 */
public class MainBoard extends JPanel{
    protected ArrayList<Integer> path;
    protected int generation;
    protected double average;
    protected double diversity;
    protected double fitestScore;
    protected int fitestSize;

    public MainBoard(){
        setBorder(BorderFactory.createLineBorder(Color.black));
    }

    public void setPath(ArrayList<Integer> path){
        this.path=path;
    }

    public void setGeneration(int generation){
        this.generation=generation;
    }

    public void setAverage(double average){
        this.average=average;
    }
    
    public void setDiversity(double diversity) {
        this.diversity=diversity;
    }

    public void setFitestScore(double fiestScore) {
        this.fitestScore = fiestScore;
    }

    public void setFitestSize(int fitestSize) {
        this.fitestSize = fitestSize;
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(1024,800);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Maze maze = Maze.getInstance();
        byte[] grid = maze.getGrid().clone();
        int size = maze.getSize();

        if(path!=null){
            int last=0;
            for(int pos : path){
                grid[pos]=4;
                last=pos;
            }
            grid[last]=5;
        }

        //upperwall
        for(int x = 0; x < size+2; x++)
            this.drawWall(g, x, 0);

        for(int y = 1; y < size+1; y++){
            for(int x = 0; x < size+2; x++){
                if(x==0){
                    this.drawWall(g, x, y);
                }else if(x==size+1){
                    this.drawWall(g, x, y);
                }else{
                    byte field = grid[x-1+(y-1)*size];
                    if(field == Maze.EMPTY_FIELD)
                        this.drawEmpty(g, x, y);
                    if(field == Maze.WALL_FIELD)
                        this.drawWall(g, x, y);
                    if(field == Maze.STARTING_POSITION)
                        this.drawStart(g,x,y);
                    if(field == Maze.MAZE_EXIT)
                        this.drawExit(g,x,y);
                    else if(field == 4)
                        this.drawPath(g, x, y);
                    else if(field == 5)
                        this.drawLastMove(g, x, y);
                }
            }
        }

        for(int x = 0; x < size+2; x++)
            this.drawWall(g, x, size+1);

        g.drawString("Generation : "+generation,700,20);
        g.drawString("Diversity : "+diversity, 700, 40);
        g.drawString("Average score : "+average, 700, 60);
        g.drawString("Fitest score : "+fitestScore, 700, 80);
        g.drawString("Fitest size : "+fitestSize, 700, 100);
    }

    private void drawWall(Graphics g, int x, int y){
        g.setColor(Color.DARK_GRAY);
        g.fillRect(x*20,y*20,20,20);
        g.setColor(Color.BLACK);
        g.drawRect(x*20,y*20,20,20);
    }

    private void drawEmpty(Graphics g, int x, int y){
        g.setColor(Color.WHITE);
        g.fillRect(x*20,y*20,20,20);
        g.setColor(Color.BLACK);
        g.drawRect(x*20,y*20,20,20);
    }

    private void drawStart(Graphics g, int x, int y) {
        g.setColor(Color.YELLOW);
        g.fillRect(x*20,y*20,20,20);
        g.setColor(Color.BLACK);
        g.drawRect(x*20,y*20,20,20);
    }

    private void drawExit(Graphics g, int x, int y) {
        g.setColor(Color.RED);
        g.fillRect(x*20,y*20,20,20);
        g.setColor(Color.BLACK);
        g.drawRect(x*20,y*20,20,20);
    }

    private void drawPath(Graphics g, int x, int y) {
        g.setColor(Color.BLUE);
        g.fillRect(x*20,y*20,20,20);
        g.setColor(Color.BLACK);
        g.drawRect(x*20,y*20,20,20);
    }

    private void drawLastMove(Graphics g, int x, int y) {
        g.setColor(Color.ORANGE);
        g.fillRect(x*20,y*20,20,20);
        g.setColor(Color.BLACK);
        g.drawRect(x*20,y*20,20,20);
    }
}
